using GraphQL.DataLoader;
using GraphQL.Types;
using GraphQLDemo.Domain;
using GraphQLDemo.Domain.Contracts;

namespace GraphQLDemo.Api.GroupTypes;

public class ContractPageGraphType : ObjectGraphType<Contract>
{
    public ContractPageGraphType(IDataLoaderContextAccessor accessor, IContractRepository contractRepository)
    {
        Name = "ContractPage";
        Description = "contract page test ... ";

        Field(c => c.Id);
        Field(c => c.Name);
        Field<IntGraphType>("itemCount")
            .Resolve(context =>
            {
                var loader = accessor.Context.GetOrAddBatchLoader<Guid, int>("GetItemsCountById", contractRepository.GetItemsCountByIdAsync);
                return loader.LoadAsync(context.Source.Id);
            });
    }
}